{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "import cudaq\n",
    "from cudaq import spin\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import torch\n",
    "from torch.autograd import Function\n",
    "from torchvision import datasets, transforms\n",
    "import torch.optim as optim\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "torch.manual_seed(22)\n",
    "cudaq.set_random_seed(44)\n",
    "\n",
    "# device = torch.device('cpu')\n",
    "# cudaq.set_target(\"qpp-cpu\")\n",
    "\n",
    "cudaq.set_target(\"nvidia\") # nvidia-mqpu\n",
    "device = torch.device(\"cuda\")\n",
    "\n",
    "print(cudaq.num_available_gpus())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j]\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "module 'cudaq' has no attribute 'from_state'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 9\u001b[0m\n\u001b[1;32m      5\u001b[0m state[\u001b[38;5;241m7\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1.\u001b[39m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(state)\n\u001b[0;32m----> 9\u001b[0m kernel \u001b[38;5;241m=\u001b[39m \u001b[43mcudaq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_state\u001b[49m(state)\n\u001b[1;32m     10\u001b[0m \u001b[38;5;66;03m#extracted_state = cudaq.get_state(kernel) \u001b[39;00m\n\u001b[1;32m     11\u001b[0m \u001b[38;5;66;03m#print(extracted_state)\u001b[39;00m\n\u001b[1;32m     12\u001b[0m result \u001b[38;5;241m=\u001b[39m cudaq\u001b[38;5;241m.\u001b[39msample(kernel)\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'cudaq' has no attribute 'from_state'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "N = 3\n",
    "state = np.zeros(2**N, dtype=complex)\n",
    "state[7] = 1.\n",
    "print(state)\n",
    "   \n",
    "\n",
    "kernel = cudaq.from_state(state)\n",
    "#extracted_state = cudaq.get_state(kernel) \n",
    "#print(extracted_state)\n",
    "result = cudaq.sample(kernel)\n",
    "print(result) # you get |110> instead of |111>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class QuantumFunction(Function):\n",
    "    \"\"\"Allows the quantum circuit to input data, output expectation values\n",
    "    and calculate gradients of variational parameters via finite difference\"\"\"\n",
    "\n",
    "    def __init__(self, qubit_count: int, hamiltonian: cudaq.SpinOperator):\n",
    "        \"\"\"Define the quantum circuit in CUDA Quantum\"\"\"\n",
    "\n",
    "        @cudaq.kernel\n",
    "        def kernel(qubit_count: int, thetas: np.ndarray):\n",
    "            qubits = cudaq.qvector(qubit_count)\n",
    "            ry(thetas[0], qubits[0])\n",
    "            rx(thetas[1], qubits[0])\n",
    "\n",
    "        self.kernel = kernel\n",
    "        self.qubit_count = qubit_count\n",
    "        self.hamiltonian = hamiltonian\n",
    "\n",
    "    def run(self, theta_vals: torch.tensor) -> torch.tensor:\n",
    "        \"\"\"Excetute the quantum circuit to output an expectation value\"\"\"\n",
    "\n",
    "        #If running on GPU, thetas is a torch.tensor that will live on GPU memory. The observe function calls a .tolist() method on inputs which moves thetas from GPU to CPU.\n",
    "\n",
    "        qubit_count = [self.qubit_count for _ in range(theta_vals.shape[0])]\n",
    "\n",
    "        results = cudaq.observe(self.kernel, self.hamiltonian, qubit_count,\n",
    "                                theta_vals)\n",
    "\n",
    "        exp_vals = [results[i].expectation() for i in range(len(results))]\n",
    "        exp_vals = torch.Tensor(exp_vals).to(device)\n",
    "\n",
    "        return exp_vals\n",
    "\n",
    "    @staticmethod\n",
    "    def forward(ctx, thetas: torch.tensor, quantum_circuit,\n",
    "                shift) -> torch.tensor:\n",
    "\n",
    "        # Save shift and quantum_circuit in context to use in backward.\n",
    "        ctx.shift = shift\n",
    "        ctx.quantum_circuit = quantum_circuit\n",
    "\n",
    "        # Calculate expectation value.\n",
    "        exp_vals = ctx.quantum_circuit.run(thetas).reshape(-1, 1)\n",
    "\n",
    "        ctx.save_for_backward(thetas, exp_vals)\n",
    "\n",
    "        return exp_vals\n",
    "\n",
    "    @staticmethod\n",
    "    def backward(ctx, grad_output):\n",
    "        \"\"\"Backward pass computation via finite difference\"\"\"\n",
    "\n",
    "        thetas, _ = ctx.saved_tensors\n",
    "\n",
    "        gradients = torch.zeros(thetas.shape, device=device)\n",
    "\n",
    "        for i in range(thetas.shape[1]):\n",
    "\n",
    "            thetas_plus = thetas.clone()\n",
    "            thetas_plus[:, i] += ctx.shift\n",
    "            exp_vals_plus = ctx.quantum_circuit.run(thetas_plus)\n",
    "\n",
    "            thetas_minus = thetas.clone()\n",
    "            thetas_minus[:, i] -= ctx.shift\n",
    "            exp_vals_minus = ctx.quantum_circuit.run(thetas_minus)\n",
    "\n",
    "            gradients[:, i] = (exp_vals_plus - exp_vals_minus) / (2 * ctx.shift)\n",
    "\n",
    "        gradients = torch.mul(grad_output, gradients)\n",
    "\n",
    "        return gradients, None, None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.05565613 -1.56922158  0.00834206  1.75765741 -1.76210499  1.83760909]\n",
      " [-0.55921046 -0.37518948  1.10335608 -1.03629477  0.99023608  0.84001287]\n",
      " [-1.21844592  0.26831482 -0.3886333   1.22784802 -1.56450877  1.05183222]]\n",
      "0.0254798-0.0952863j -0.611035-0.346426j -0.424929+0.460481j -0.216561+0.239562j \n",
      "\n",
      "<class 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.State'>\n",
      "[ 0.02547981-0.09528627j -0.61103505-0.34642565j -0.42492929+0.46048146j\n",
      " -0.21656093+0.23956227j]\n",
      "[0.09863414 0.70240627 0.62658446 0.32293764]\n",
      "1.7505624998591665\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# Define our kernel.\n",
    "@cudaq.kernel\n",
    "def generator_kernel(qubit_count: int, thetas: np.ndarray, layers: int):\n",
    "    # Allocate our qubits.\n",
    "    qubits = cudaq.qvector(qubit_count)\n",
    "    theta_count = 0\n",
    "    for _ in range(layers):\n",
    "        for ix in range(qubit_count):\n",
    "            ry(thetas[theta_count], qubits[ix])\n",
    "            theta_count += 1\n",
    "            rx(thetas[theta_count], qubits[ix])\n",
    "            theta_count += 1\n",
    "            rz(thetas[theta_count], qubits[ix])\n",
    "            theta_count += 1\n",
    "        for ix in range(qubit_count - 1):\n",
    "            x.ctrl(qubits[ix], qubits[ix + 1])\n",
    "    # mz(qubits)\n",
    "\n",
    "qubit_count = 2\n",
    "layers = 3\n",
    "thetas = np.random.normal(0, 1, size=[layers, qubit_count*3])\n",
    "print(thetas)\n",
    "\n",
    "# print(cudaq.draw(generator_kernel, qubit_count, thetas.reshape(-1), layers))D\n",
    "qstate = cudaq.get_state(generator_kernel, qubit_count, thetas.reshape(-1), layers)\n",
    "print(qstate)\n",
    "print(type(qstate))\n",
    "a = np.array(qstate)\n",
    "print(a)\n",
    "print(np.abs(a))\n",
    "print(np.sum(np.abs(a)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' object has no attribute 'amplitudes'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mqstate\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mamplitudes\u001b[49m()\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' object has no attribute 'amplitudes'"
     ]
    }
   ],
   "source": [
    "qstate.amplitudes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# qstate_2 = cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.get_state(generator_kernel, qubit_count, thetas.reshape(-1), layers)\n",
    "orthogonal_state = np.zeros(2**qubit_count, dtype=np.complex128)\n",
    "orthogonal_state[1] = 1\n",
    "qstate_2 = cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.State(orthogonal_state)\n",
    "cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.State.overlap(qstate_2,qstate_2)\n",
    "# qstate_2.dump()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2768849878496445\n",
      "0.6842258617831293\n",
      "0.31856297904373315\n",
      "0.5947163379547546\n"
     ]
    }
   ],
   "source": [
    "for i in range(2**qubit_count):\n",
    "    orthogonal_state = np.zeros(2**qubit_count, dtype=np.complex128)\n",
    "    orthogonal_state[i] = 1\n",
    "    print(qstate.overlap((orthogonal_state)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "type object 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' has no attribute 'State'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m c \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39mj, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39mj, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39mj, \u001b[38;5;241m7\u001b[39m, \u001b[38;5;241m8\u001b[39mj], dtype\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39mcomplex64)\n\u001b[0;32m----> 2\u001b[0m state \u001b[38;5;241m=\u001b[39m \u001b[43mcudaq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mState\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mState\u001b[49m\u001b[38;5;241m.\u001b[39mfrom_data(c)\n\u001b[1;32m      3\u001b[0m \u001b[38;5;66;03m# state = cudaq.State.from_data(c)\u001b[39;00m\n\u001b[1;32m      5\u001b[0m cudaq\u001b[38;5;241m.\u001b[39mState\n",
      "\u001b[0;31mAttributeError\u001b[0m: type object 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' has no attribute 'State'"
     ]
    }
   ],
   "source": [
    "c = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j], dtype=np.complex64)\n",
    "state = cudaq.State.State.from_data(c)\n",
    "# state = cudaq.State.from_data(c)\n",
    "\n",
    "cudaq.State"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' object has no attribute 'ToTensor'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[100], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m s2 \u001b[38;5;241m=\u001b[39m cudaq\u001b[38;5;241m.\u001b[39mState(state)\n\u001b[0;32m----> 2\u001b[0m \u001b[43ms2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mToTensor\u001b[49m()\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime' object has no attribute 'ToTensor'"
     ]
    }
   ],
   "source": [
    "s2 = cudaq.State(state)\n",
    "s2.ToTensor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "object of type 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.State' has no len()",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[79], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstate\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: object of type 'cudaq.mlir._mlir_libs._quakeDialects.cudaq_runtime.State' has no len()"
     ]
    }
   ],
   "source": [
    "state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.29118993 -1.21485278  0.1925102  -0.40727453 -0.11457405  0.96741856\n",
      "   1.00384274  0.87515969 -1.30872092 -0.13819804]\n",
      " [ 0.38545717  0.40496696 -1.26578799 -0.24007079 -1.34706637  0.51291888\n",
      "  -1.02981805 -0.71936201 -0.09823871  0.20776848]\n",
      " [-0.51931479 -0.34918431  0.44556586 -0.21426969  0.6627725   1.67861425\n",
      "   0.5048656   1.2231496  -1.27188848 -0.45908467]]\n",
      "0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j -0.976997+0.213255j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j 0+0j \n",
      "\n"
     ]
    }
   ],
   "source": [
    "@cudaq.kernel\n",
    "def generator_kernel(qubit_count: int, thetas: np.ndarray, layers: int):\n",
    "    # Allocate our qubits.\n",
    "    qubits = cudaq.qvector(qubit_count)\n",
    "    theta_count = 0\n",
    "    for _ in range(layers):\n",
    "        for ix in range(qubit_count):\n",
    "            ry(thetas[theta_count], qubits[ix])\n",
    "            theta_count += 1\n",
    "            rx(thetas[theta_count], qubits[ix])\n",
    "            theta_count += 1\n",
    "        for ix in range(qubit_count - 1):\n",
    "            x.ctrl(qubits[ix], qubits[ix + 1])\n",
    "    mz(qubits)\n",
    "\n",
    "qubit_count = 5\n",
    "layers = 3\n",
    "thetas = np.random.normal(0, 1, size=[layers, qubit_count*2])\n",
    "print(thetas)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j]\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "module 'cudaq' has no attribute 'from_state'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[74], line 9\u001b[0m\n\u001b[1;32m      5\u001b[0m state[\u001b[38;5;241m7\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1.\u001b[39m \n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(state)\n\u001b[0;32m----> 9\u001b[0m kernel \u001b[38;5;241m=\u001b[39m \u001b[43mcudaq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_state\u001b[49m(state)\n\u001b[1;32m     10\u001b[0m \u001b[38;5;66;03m#extracted_state = cudaq.get_state(kernel) \u001b[39;00m\n\u001b[1;32m     11\u001b[0m \u001b[38;5;66;03m#print(extracted_state)\u001b[39;00m\n\u001b[1;32m     12\u001b[0m result \u001b[38;5;241m=\u001b[39m cudaq\u001b[38;5;241m.\u001b[39msample(kernel)\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'cudaq' has no attribute 'from_state'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "N = 3\n",
    "state = np.zeros(2**N, dtype=complex)\n",
    "state[7] = 1. \n",
    "print(state)\n",
    "   \n",
    "\n",
    "kernel = cudaq.from_state(state)\n",
    "#extracted_state = cudaq.get_state(kernel) \n",
    "#print(extracted_state)\n",
    "result = cudaq.sample(kernel)\n",
    "print(result) # you get |110> instead of |111>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "most probable = 00010\n",
      "expectation_value = -0.09399999999999997\n",
      "{ 11111:47 01111:12 01010:11 01110:10 01101:63 10010:80 00010:173 11010:37 00100:21 00110:42 10101:16 11000:42 01000:16 00111:70 11100:6 10111:18 10000:6 11011:25 10100:8 10110:5 11110:36 00001:25 10001:7 00000:23 01001:8 11101:38 11001:12 00101:19 01011:9 00011:60 01100:24 10011:31 }\n",
      "\n"
     ]
    }
   ],
   "source": [
    "sample_result = cudaq.sample(generator_kernel, qubit_count, thetas.reshape(-1), layers)\n",
    "print(f\"most probable = {sample_result.most_probable()}\")\n",
    "print(f\"expectation_value = {sample_result.expectation()}\")\n",
    "print(sample_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m"
     ]
    },
    {
     "ename": "CompilerError",
     "evalue": "696875955.py:7: \u001b[91merror: \u001b[0m\u001b[1munhandled subscript\n\t (offending source -> thetas[il][ix])\u001b[0m",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mCompilerError\u001b[0m                             Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[24], line 9\u001b[0m\n\u001b[1;32m      7\u001b[0m             ry(thetas[il][ix], qubits[il][ix])\n\u001b[1;32m      8\u001b[0m         \u001b[38;5;66;03m# rx(thetas[ix], qubits[ix])\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mcudaq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtest_kernel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/kernel_decorator.py:158\u001b[0m, in \u001b[0;36mPyKernelDecorator.compile\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    155\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodule \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    156\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m--> 158\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodule, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39margTypes, extraMetadata \u001b[38;5;241m=\u001b[39m \u001b[43mcompile_to_mlir\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    159\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastModule\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    160\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    161\u001b[0m \u001b[43m    \u001b[49m\u001b[43mverbose\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mverbose\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    162\u001b[0m \u001b[43m    \u001b[49m\u001b[43mreturnType\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreturnType\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    163\u001b[0m \u001b[43m    \u001b[49m\u001b[43mlocation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlocation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    164\u001b[0m \u001b[43m    \u001b[49m\u001b[43mparentVariables\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mglobalScopedVars\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    166\u001b[0m \u001b[38;5;66;03m# Grab the dependent capture variables, if any\u001b[39;00m\n\u001b[1;32m    167\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdependentCaptures \u001b[38;5;241m=\u001b[39m extraMetadata[\n\u001b[1;32m    168\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdependent_captures\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdependent_captures\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m extraMetadata \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:3031\u001b[0m, in \u001b[0;36mcompile_to_mlir\u001b[0;34m(astModule, metadata, **kwargs)\u001b[0m\n\u001b[1;32m   3026\u001b[0m         PyASTBridge(existingModule\u001b[38;5;241m=\u001b[39mbridge\u001b[38;5;241m.\u001b[39mmodule,\n\u001b[1;32m   3027\u001b[0m                     locationOffset\u001b[38;5;241m=\u001b[39mdepKernels[funcName][\u001b[38;5;241m1\u001b[39m])\u001b[38;5;241m.\u001b[39mvisit(\n\u001b[1;32m   3028\u001b[0m                         depKernels[funcName][\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m   3030\u001b[0m \u001b[38;5;66;03m# Build the MLIR Module for this kernel\u001b[39;00m\n\u001b[0;32m-> 3031\u001b[0m \u001b[43mbridge\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mastModule\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3033\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m verbose:\n\u001b[1;32m   3034\u001b[0m     \u001b[38;5;28mprint\u001b[39m(bridge\u001b[38;5;241m.\u001b[39mmodule)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:634\u001b[0m, in \u001b[0;36mPyASTBridge.generic_visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    632\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m value:\n\u001b[1;32m    633\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(item, ast\u001b[38;5;241m.\u001b[39mAST):\n\u001b[0;32m--> 634\u001b[0m             \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mitem\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    635\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(value, ast\u001b[38;5;241m.\u001b[39mAST):\n\u001b[1;32m    636\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(value)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:739\u001b[0m, in \u001b[0;36mPyASTBridge.visit_FunctionDef\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    737\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(constant\u001b[38;5;241m.\u001b[39mvalue, \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m    738\u001b[0m             startIdx \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m--> 739\u001b[0m [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(n) \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody[startIdx:]]\n\u001b[1;32m    740\u001b[0m \u001b[38;5;66;03m# Add the return operation\u001b[39;00m\n\u001b[1;32m    741\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhasTerminator(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mentry):\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:739\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m    737\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(constant\u001b[38;5;241m.\u001b[39mvalue, \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m    738\u001b[0m             startIdx \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m--> 739\u001b[0m [\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mn\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody[startIdx:]]\n\u001b[1;32m    740\u001b[0m \u001b[38;5;66;03m# Add the return operation\u001b[39;00m\n\u001b[1;32m    741\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhasTerminator(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mentry):\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2214\u001b[0m, in \u001b[0;36mPyASTBridge.visit_For\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m   2211\u001b[0m             [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(b) \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m             \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n\u001b[0;32m-> 2214\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreateInvariantForLoop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mendVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2215\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mbodyBuilder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2216\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstartVal\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstartVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2217\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstepVal\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstepVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2218\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43misDecrementing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43misDecrementing\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2219\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m   2221\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(node\u001b[38;5;241m.\u001b[39miter)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:528\u001b[0m, in \u001b[0;36mPyASTBridge.createInvariantForLoop\u001b[0;34m(self, endVal, bodyBuilder, startVal, stepVal, isDecrementing)\u001b[0m\n\u001b[1;32m    526\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m    527\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpushForBodyStack(bodyBlock\u001b[38;5;241m.\u001b[39marguments)\n\u001b[0;32m--> 528\u001b[0m \u001b[43mbodyBuilder\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbodyBlock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marguments\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    529\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhasTerminator(bodyBlock):\n\u001b[1;32m    530\u001b[0m     cc\u001b[38;5;241m.\u001b[39mContinueOp(bodyBlock\u001b[38;5;241m.\u001b[39marguments)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2211\u001b[0m, in \u001b[0;36mPyASTBridge.visit_For.<locals>.bodyBuilder\u001b[0;34m(iterVar)\u001b[0m\n\u001b[1;32m   2209\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m   2210\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39madd(node\u001b[38;5;241m.\u001b[39mtarget\u001b[38;5;241m.\u001b[39mid, iterVar)\n\u001b[0;32m-> 2211\u001b[0m [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(b) \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2211\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m   2209\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m   2210\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39madd(node\u001b[38;5;241m.\u001b[39mtarget\u001b[38;5;241m.\u001b[39mid, iterVar)\n\u001b[0;32m-> 2211\u001b[0m [\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2214\u001b[0m, in \u001b[0;36mPyASTBridge.visit_For\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m   2211\u001b[0m             [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(b) \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m             \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n\u001b[0;32m-> 2214\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreateInvariantForLoop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mendVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2215\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mbodyBuilder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2216\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstartVal\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstartVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2217\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstepVal\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstepVal\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2218\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43misDecrementing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43misDecrementing\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2219\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m   2221\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(node\u001b[38;5;241m.\u001b[39miter)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:528\u001b[0m, in \u001b[0;36mPyASTBridge.createInvariantForLoop\u001b[0;34m(self, endVal, bodyBuilder, startVal, stepVal, isDecrementing)\u001b[0m\n\u001b[1;32m    526\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m    527\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpushForBodyStack(bodyBlock\u001b[38;5;241m.\u001b[39marguments)\n\u001b[0;32m--> 528\u001b[0m \u001b[43mbodyBuilder\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbodyBlock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marguments\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    529\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhasTerminator(bodyBlock):\n\u001b[1;32m    530\u001b[0m     cc\u001b[38;5;241m.\u001b[39mContinueOp(bodyBlock\u001b[38;5;241m.\u001b[39marguments)\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2211\u001b[0m, in \u001b[0;36mPyASTBridge.visit_For.<locals>.bodyBuilder\u001b[0;34m(iterVar)\u001b[0m\n\u001b[1;32m   2209\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m   2210\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39madd(node\u001b[38;5;241m.\u001b[39mtarget\u001b[38;5;241m.\u001b[39mid, iterVar)\n\u001b[0;32m-> 2211\u001b[0m [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(b) \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2211\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m   2209\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpushScope()\n\u001b[1;32m   2210\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39madd(node\u001b[38;5;241m.\u001b[39mtarget\u001b[38;5;241m.\u001b[39mid, iterVar)\n\u001b[0;32m-> 2211\u001b[0m [\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m b \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39mbody]\n\u001b[1;32m   2212\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msymbolTable\u001b[38;5;241m.\u001b[39mpopScope()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:775\u001b[0m, in \u001b[0;36mPyASTBridge.visit_Expr\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    772\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(constant\u001b[38;5;241m.\u001b[39mvalue, \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m    773\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m--> 775\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:1061\u001b[0m, in \u001b[0;36mPyASTBridge.visit_Call\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m   1057\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m   1059\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(node\u001b[38;5;241m.\u001b[39mfunc, ast\u001b[38;5;241m.\u001b[39mName):\n\u001b[1;32m   1060\u001b[0m     \u001b[38;5;66;03m# Just visit the arguments, we know the name\u001b[39;00m\n\u001b[0;32m-> 1061\u001b[0m     [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvisit(arg) \u001b[38;5;28;01mfor\u001b[39;00m arg \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39margs]\n\u001b[1;32m   1062\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m node\u001b[38;5;241m.\u001b[39mfunc\u001b[38;5;241m.\u001b[39mid \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlen\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m   1063\u001b[0m         listVal \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpopValue()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:1061\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m   1057\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m   1059\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(node\u001b[38;5;241m.\u001b[39mfunc, ast\u001b[38;5;241m.\u001b[39mName):\n\u001b[1;32m   1060\u001b[0m     \u001b[38;5;66;03m# Just visit the arguments, we know the name\u001b[39;00m\n\u001b[0;32m-> 1061\u001b[0m     [\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvisit\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m arg \u001b[38;5;129;01min\u001b[39;00m node\u001b[38;5;241m.\u001b[39margs]\n\u001b[1;32m   1062\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m node\u001b[38;5;241m.\u001b[39mfunc\u001b[38;5;241m.\u001b[39mid \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlen\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m   1063\u001b[0m         listVal \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpopValue()\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/ast.py:418\u001b[0m, in \u001b[0;36mNodeVisitor.visit\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m    416\u001b[0m method \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvisit_\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m+\u001b[39m node\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m    417\u001b[0m visitor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, method, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mgeneric_visit)\n\u001b[0;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvisitor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:2182\u001b[0m, in \u001b[0;36mPyASTBridge.visit_Subscript\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m   2179\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpushValue(cc\u001b[38;5;241m.\u001b[39mLoadOp(eleAddr)\u001b[38;5;241m.\u001b[39mresult)\n\u001b[1;32m   2180\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[0;32m-> 2182\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43memitFatalError\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43munhandled subscript\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnode\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/.conda/envs/cuda-quantum/lib/python3.10/site-packages/cudaq/kernel/ast_bridge.py:169\u001b[0m, in \u001b[0;36mPyASTBridge.emitFatalError\u001b[0;34m(self, msg, astNode)\u001b[0m\n\u001b[1;32m    163\u001b[0m \u001b[38;5;28mprint\u001b[39m(Color\u001b[38;5;241m.\u001b[39mBOLD, end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m    164\u001b[0m msg \u001b[38;5;241m=\u001b[39m codeFile \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mstr\u001b[39m(\n\u001b[1;32m    165\u001b[0m     lineNumber\n\u001b[1;32m    166\u001b[0m ) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m: \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m Color\u001b[38;5;241m.\u001b[39mRED \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merror: \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m Color\u001b[38;5;241m.\u001b[39mEND \u001b[38;5;241m+\u001b[39m Color\u001b[38;5;241m.\u001b[39mBOLD \u001b[38;5;241m+\u001b[39m msg \u001b[38;5;241m+\u001b[39m (\n\u001b[1;32m    167\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;124m (offending source -> \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m ast\u001b[38;5;241m.\u001b[39munparse(astNode) \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m\n\u001b[1;32m    168\u001b[0m     \u001b[38;5;28mhasattr\u001b[39m(ast, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124munparse\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m astNode \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m+\u001b[39m Color\u001b[38;5;241m.\u001b[39mEND\n\u001b[0;32m--> 169\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CompilerError(msg)\n",
      "\u001b[0;31mCompilerError\u001b[0m: 696875955.py:7: \u001b[91merror: \u001b[0m\u001b[1munhandled subscript\n\t (offending source -> thetas[il][ix])\u001b[0m"
     ]
    }
   ],
   "source": [
    "@cudaq.kernel\n",
    "def test_kernel(qubit_count: int, thetas: np.ndarray, layers: int):\n",
    "\n",
    "    qubits = cudaq.qvector(qubit_count)\n",
    "    for il in range(layers):\n",
    "        for ix in range(qubit_count):\n",
    "            ry(thetas[il][ix], qubits[il][ix])\n",
    "        # rx(thetas[ix], qubits[ix])\n",
    "print(cudaq.draw(test_kernel, 2, np.zeros((2,2)), 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.66339776,  0.45141048])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.normal(0, 1, size=[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.07284855593608636"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qubit_count = 5\n",
    "layers = 3\n",
    "thetas = np.random.normal(0, 1, size=[layers, qubit_count*2])\n",
    "thetas[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cuda-quantum",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
